package java_thinking.unit_17._17_9_10;

import java_thinking.unit_17._17_2_6.Countries;

import java.util.LinkedList;

import static java_thinking.net.mindview.util.Print.print;

/**
 * @Desc:令SimpleHashMap实现完整的Map接口
 * @author: cww
 * @DateTime: 2019-04-19 22:19
 */

public class SimpleHashMap3<K,V> extends SimpleHashMap2<K,V> {
    @Override
    public int size() {
        int sz = 0;
        for (LinkedList<SlowMap.MapEntry<K,V>> entries : buckets) {
            if (entries != null) {
                sz += entries.size();
            }
        }
        return sz;
    }

    @Override
    public boolean isEmpty() {
        for (LinkedList<SlowMap.MapEntry<K,V>> entries : buckets) {
            if (entries != null) {
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean containsKey(Object key) {
        for (LinkedList<SlowMap.MapEntry<K,V>> entries : buckets) {
            if (entries == null) {
                continue;
            }
            for (SlowMap.MapEntry<K,V> kvMapEntry : entries) {
                if (kvMapEntry.getKey().equals(key)) {
                    return true;
                }
            }
        }
        return false;
    }

    public static void main(String[] args) {
        SimpleHashMap3<String,String> m = new SimpleHashMap3<String,String>(),
                m2 = new SimpleHashMap3<String,String>();
        m.putAll(Countries.capitals(10));
        m2.putAll(Countries.capitals(10));
        print("m.size() = " + m.size());
        print("m.isEmpty() = " + m.isEmpty());
        print("m.equals(m2) = " + m.equals(m2));
        print("m.containsKey(\"BENIN\") = " +
                m.containsKey("BENIN"));
        print("m.containsKey(\"MARS\") = " +
                m.containsKey("MARS"));
        print("m.keySet() = " + m.keySet());
        print("m.values() = " + m.values());
    }
}
